﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ScheduleMaker.DB.Domain;
using ScheduleMaker.DB.Domain.Core;

namespace ScheduleMaker.DB.CoreDAO
{
    public class EmergencyScheduleDAO : CoreAbstractDAO<EmergencySchedule>
    {
        private AdapterAbstractDAO<EmergencySchedule, EmergencyScheduleSchool> wrappedAbstractDAOSchool;
        private AdapterAbstractDAO<EmergencySchedule, EmergencyScheduleUniversity> wrappedAbstractDAOUniversity;
        private EmergencyScheduleDAOSchool wrappedDAOSchool;
        private EmergencyScheduleDAOUniversity wrappedDAOUniversity;

        public EmergencyScheduleDAO(EmergencyScheduleDAOSchool wrappedDAOSchool)
        {
            this.wrappedDAOSchool = wrappedDAOSchool;
            this.wrappedAbstractDAOSchool = 
                new AdapterAbstractDAO<EmergencySchedule,EmergencyScheduleSchool>((AbstractDAO<EmergencyScheduleSchool>)wrappedDAOSchool);
        }

        public EmergencyScheduleDAO(EmergencyScheduleDAOUniversity wrappedDAOUniversity)
        {
            this.wrappedDAOUniversity = wrappedDAOUniversity;
            this.wrappedAbstractDAOUniversity =
                new AdapterAbstractDAO<EmergencySchedule, EmergencyScheduleUniversity>((AbstractDAO<EmergencyScheduleUniversity>)wrappedDAOUniversity);
        }

        public IList<EmergencySchedule> getByDate(DateTime date)
        {
            if (wrappedDAOSchool != null)
            {
                List<EmergencyScheduleSchool> list = (List<EmergencyScheduleSchool>)wrappedDAOSchool.getEmergencyScheduleByDate(date);
                return list.ConvertAll(x => (EmergencySchedule)x);
            }
            else if (wrappedDAOUniversity != null)
            {
                List<EmergencyScheduleUniversity> list = (List<EmergencyScheduleUniversity>)wrappedDAOUniversity.getEmergencyScheduleByDate(date);
                return list.ConvertAll(x => (EmergencySchedule)x);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped EmergencyScheduleDAOSchool nor EmergencyScheduleDAOUniversity was initialized");
            }
        }

        public IList<EmergencySchedule> getByClassroom(Classroom classroom)
        {
            if (wrappedDAOSchool != null)
            {
                List<EmergencyScheduleSchool> list = (List<EmergencyScheduleSchool>)wrappedDAOSchool.getByClassroom(classroom);
                return list.ConvertAll(x => (EmergencySchedule)x);
            }
            else if (wrappedDAOUniversity != null)
            {
                List<EmergencyScheduleUniversity> list = (List<EmergencyScheduleUniversity>)wrappedDAOUniversity.getByClassroom(classroom);
                return list.ConvertAll(x => (EmergencySchedule)x);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped EmergencyScheduleDAOSchool nor EmergencyScheduleDAOUniversity was initialized");
            }
        }

        public IList<EmergencySchedule> getByTeacher(Teacher teacher)
        {
            if (wrappedDAOSchool != null)
            {
                List<EmergencyScheduleSchool> list = (List<EmergencyScheduleSchool>)wrappedDAOSchool.getByTeacher(teacher);
                return list.ConvertAll(x => (EmergencySchedule)x);
            }
            else if (wrappedDAOUniversity != null)
            {
                List<EmergencyScheduleUniversity> list = (List<EmergencyScheduleUniversity>)wrappedDAOUniversity.getByTeacher(teacher);
                return list.ConvertAll(x => (EmergencySchedule)x);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped EmergencyScheduleDAOSchool nor EmergencyScheduleDAOUniversity was initialized");
            }
        }

        public IList<EmergencySchedule> getByGroup(Group group)
        {
            if (wrappedDAOSchool != null)
            {
                List<EmergencyScheduleSchool> list = (List<EmergencyScheduleSchool>)wrappedDAOSchool.getByGroup(group);
                return list.ConvertAll(x => (EmergencySchedule)x);
            }
            else if (wrappedDAOUniversity != null)
            {
                throw new NotSupportedMethodException();
            }
            else
            {
                throw new NotImplementedException("Neither wrapped EmergencyScheduleDAOSchool nor EmergencyScheduleDAOUniversity was initialized");
            }
        }

        public override void update(EmergencySchedule entity)
        {
            if (wrappedDAOSchool != null)
            {
                wrappedAbstractDAOSchool.update(entity);
            }
            else if (wrappedDAOUniversity != null)
            { 
                wrappedAbstractDAOUniversity.update(entity); 
            }
            else
            {
                throw new NotImplementedException("Neither wrapped EmergencyScheduleDAOSchool nor EmergencyScheduleDAOUniversity was initialized");
            }
        }

        public override void save(EmergencySchedule entity)
        {
            if (wrappedDAOSchool != null)
            {
                wrappedAbstractDAOSchool.save(entity);
            }
            else if (wrappedDAOUniversity != null)
            {
                wrappedAbstractDAOUniversity.save(entity);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped EmergencyScheduleDAOSchool nor EmergencyScheduleDAOUniversity was initialized");
            }
        }

        public override void saveOrUpdate(EmergencySchedule entity)
        {
            if (wrappedDAOSchool != null)
            {
                wrappedAbstractDAOSchool.saveOrUpdate(entity);
            }
            else if (wrappedDAOUniversity != null)
            {
                wrappedAbstractDAOUniversity.saveOrUpdate(entity);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped EmergencyScheduleDAOSchool nor EmergencyScheduleDAOUniversity was initialized");
            }
        }

        public override void delete(EmergencySchedule entity)
        {
            if (wrappedDAOSchool != null)
            {
                wrappedAbstractDAOSchool.delete(entity);
            }
            else if (wrappedDAOUniversity != null)
            {
                wrappedAbstractDAOUniversity.delete(entity);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped EmergencyScheduleDAOSchool nor EmergencyScheduleDAOUniversity was initialized");
            }
        }

        public override EmergencySchedule getById(int id)
        {
            EmergencySchedule entity;
            if (wrappedDAOSchool != null)
            {
                entity = wrappedAbstractDAOSchool.getById(id);
            }
            else if (wrappedDAOUniversity != null)
            {
                entity = wrappedAbstractDAOUniversity.getById(id);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped EmergencyScheduleDAOSchool nor EmergencyScheduleDAOUniversity was initialized");
            }

            return entity;
        }

        public override IList<EmergencySchedule> getAll()
        {
            IList<EmergencySchedule> records;
            if (wrappedDAOSchool != null)
            {
                records = wrappedAbstractDAOSchool.getAll();
            }
            else if (wrappedDAOUniversity != null)
            {
                records = wrappedAbstractDAOUniversity.getAll();
            }
            else
            {
                throw new NotImplementedException("Neither wrapped EmergencyScheduleDAOSchool nor EmergencyScheduleDAOUniversity was initialized");
            }

            return records;
        }
    }
}
